<?php

class Database_Model_Item extends Database_Table_Row
{
	
	/**
	 * @var string data type
	 */
	protected $_itemType = '';
	
	/**
	 * @var string
	 */
	protected $_pk  = 'id';
	
	/**
	 * @var string
	 */
	function getType()
	{
		return $this -> _itemType;
	}

	/**
	 * set data type for model
	 * @param string $dataType
	 * @return Database_Table_Row
	 */
	function setItemType($itemType)
	{
		$this -> _itemType = $itemType;
		return $this;
	}
	

	/**
	 * @return int|string
	 */
	function getId()
	{
		if (isset($this -> {$this->_pk}))
		{
			return $this -> {$this->_pk};
		}
		return 0;
	}

	/**
	 * global unique idenity cross all table
	 * @return string
	 */
	function getItemId()
	{
		return $this -> getType() . '_' . $this -> getId();
	}
	
	/**
	 * @return string
	 */
	function getHtmlId($prefix='ui')
	{
		return $prefix . '-'. $this -> getType() . '-' . $this -> getId();
	}


	public function getTitle()
	{
		return isset($this -> title) ? $this -> title : '';
	}

	public function getDescription()
	{
		return isset($this -> description) ? $this -> description : '';
	}
	
	function getUrl()
	{
		return '#';
	}

	function toLink()
	{
		return strtr('<a href=":href" title=":title">:title</a>', array(
			':href' => $this -> getUrl(),
			':title' => $this -> getTitle(),
			':data-id'=> $this->getItemId(),
		));
	}
	
	function toCardhoverLink($extra = array())
	{
		return strtr('<a href=":href" hover="cardhover" data-id=":data-id" uri=":uri" class="inlineBlock :class">:title</a>', array(
			':href' => $this -> getUrl(),
			':title' => $this -> getTitle(),
			':data-id'=> $this->getItemId(),
			':class'=>isset($extra['class'])?$extra['class']:'',
			':uri'=>$this->getCardhoverUrl(),
		));	
	}
	
	function toCardhoverPhoto($pf = 's100', $extra = array())
	{
		return strtr('<a href=":href" class="inlineBlock" hover="cardhover" data-id=":data-id" uri=":uri" ><img class=":class" width=":width" height=":height" src=":src" alt=":title"/></a>', array(
			':href' => $this -> getUrl(),
			':title' => $this -> getTitle(),
			':data-id'=> $this->getItemId(),
			':class'=>isset($extra['class'])?$extra['class']:'',
			':uri'=>$this->getCardhoverUrl(),
			':src'=>$this->getPhotoUrl($pf),
			':width'=>isset($extra['width'])?$extra['width']:'',
			':height'=>isset($extra['height'])?$extra['height']:'',
		));	
	}

	
		
	function getCardhoverUrl()
	{
		return '';	
	}


	/**
	 * get param of photo
	 */
	function toPhotoLink()
	{
		return strtr('<a href="{href}" data-id="{data-id}"><img tooltip="{title}" src="{src}" alt="{title}"/></a>', array(
			'{title}' => $this -> getTitle(),
			'{src}' => $this -> getPhotoSrc($tag),
			'{href}' => $this -> getUrl(),
			'{data-id}' => $this -> getItemId(),
		));
	}
	
	/**
	 * @return string html string of <img /> tag
	 */
	function toPhoto($tag = 'm')
	{
		return strtr('<img src="{src}" alt="{title}"/>', array(
			'{title}' => $this -> getTitle(),
			'{src}' => $this -> getPhotoSrc($tag)
		));
	}

	/**
	 * text string
	 * @return string
	 */
	function toString()
	{
		return $this -> getTitle();
	}

	/**
	 * @return string
	 * get html string of object
	 */
	function __toString()
	{
		return $this -> toLink();
	}

	/**
	 * @return bool
	 */
	public function isLocked()
	{

		return isset($this -> is_locked) ? $this -> is_locked : FALSE;
	}

	/**
	 * @return bool
	 */
	public function isActive()
	{
		return isset($this -> is_active) ? $this -> is_active : FALSE;
	}

	/**
	 * @return int
	 */
	public function getSortOrder()
	{
		return isset($this -> sort_order) ? $this -> sort_order : 0;
	}

	/**
	 * @return string
	 */
	public function getPk()
	{
		return $this -> _pk;
	}	
	
	
	
	function getComments($limit = 5)
	{
		return Nuo::getApi('activity/comment')->getComments($this,$limit);
	}
	
	protected $_isLiked = NULL;
	
	/**
	 * set liked to object
	 */
	function setLiked($flag)
	{
		$this->_isLiked =  (bool)$flag;
		return $this;
	}
	/**
	 * is liked
	 * @return void
	 */
	function isLiked()
	{
		if( NULL === $this->_isLiked)
		{
			$this->_isLiked =  (bool)Nuo::getApi('activity/like')->isLiked($this, Nuo::getUser()); 
		}	
		return $this->_isLiked;
	}
	
	/**
	 *  get like message
	 */
	function getLikeMessage()
	{
	
		$count = $this -> like_count;
		
		$isLiked = $this->isLiked();

		if ($isLiked)
		{
			$count -= 1;
		}
		if ($count == 0)
		{
			if ($isLiked)
			{
				$msg = Intl::__('You like this');
			}
			else
			{
				$msg = '';
			}
		}
		else{
			$url =  Nuo::getUrl('activity/ajax/liked',array('id'=>$this->getItemId()));
			if ($count == 1)
			{
				
				if ($isLiked)
				{
					$msg = Intl::__('You and <a href=":url" hover="tooltip" aria-click="fbox">:like_count people</a> like this', array(':like_count' => $count,':url'=>$url));
				}
				else
				{
					$msg = Intl::__('<a href=":url" hover="tooltip" aria-click="fbox">:like_count people</a> like this', array(':like_count' => $count,':url'=>$url));
				}
			}
			else
			{
				if ($isLiked)
				{
					$msg = Intl::__('You and <a href=":url" hover="tooltip" aria-click="fbox">:like_count others</a> like this', array(':like_count' => $count,':url'=>$url));
				}
				else
				{
					$msg = Intl::__('<a href=":url" hover="tooltip" aria-click="fbox">:like_count people</a> like this', array(':like_count' => $count,':url'=>$url));
				}
			}
		}
		
		return $msg;
	}
	
	
	/**
	 * @return model
	 */
	function getResource()
	{
		if (isset($this -> resource_id))
		{
			return Nuo::getItem($this -> resource_id);
		}
		return NULL;

	}
	
	/**
	 * check is self
	 * @param Database_Model_Item $resource
	 * @return true/false
	 */
	function isSelf($resource)
	{
		$itemId =  $resource->getItemId();
		
		return $this->getItemId() == $resource->getItemId();
	}
	
	/**
	 * check is self
	 * @param Database_Model_Item $resource
	 * @return true/false
	 */
	function isOwner($resource)
	{
		$itemId =  $resource->getItemId();
		
		if($this->getItemId() == $itemId){
			return TRUE;
		}
		
		if(isset($this->user_id) && $this->user_id == $itemId){
			return TRUE;
		}
		
		
		if(isset($this->poster_id) && $this->poster_id  == $itemId){
			return TRUE;
		}
			
		return FALSE;
	}
		
	/**
	 * @param Database_Model_Item $poster
	 * @return Database_Model_Item
	 */
	function setPoster($poster)
	{
		$this->poster_id = $poster->getItemId();
		return $this;	
	}
	
	/**
	 * @param Database_Model_Item $poster
	 * @return Database_Model_Item
	 */
	function setResource($resource)
	{
		$this->resource_id =  $resource->getItemId();
		return $this;
	}
	
	/**
	 * @see Base_Api_Resource::checkPrivacy
	 * @param string $action
	 * @return true/false
	 */
	function isAllow($action)
	{
		static $api;
		if(null == $api){
			$api  = Nuo::getApi('privacy');
		}
		
		return $api->checkPrivacy($this, $action);
	}
	
	/**
	 * get proxy object
	 * @return Database_Model_Item
	 */
	function getProxy()
	{
		return $this;
	}
	
	function getSlug(){
		return $this->slug;
	}	
}
